package org.torproject.android;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;


public class RouterMapView extends MapActivity {

	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Debug.startMethodTracing("RouterMapView");
		
		setContentView(R.layout.layout_router_map);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		
		Intent intent = getIntent();
		circuit = (Circuit) intent.getParcelableExtra("org.torproject.android.CircuitToMap");
		
		mapOverlays = mapView.getOverlays();
		
		Drawable d = this.getResources().getDrawable(R.drawable.tor25);
		RouterItemizedOverlay rOverlay = new RouterItemizedOverlay(d, this);

		LinesItemizedOverlay lOverlay = new LinesItemizedOverlay(d, this);

		for(RouterDescriptor desc : circuit.routerDescriptors) {
			String title = desc.name;
			String snippet = "";
			String tokens[] = desc.location.split(",");
			if(tokens.length >= 4) {
				snippet += "location: " + tokens[1] +"," + tokens[2] + "," + tokens[3] + "\n";
			}
			snippet += "ip: " + desc.ip + "\n";
			snippet += "upsince: " + CircuitStatus.uptimeToString(desc.uptime) + "\n";
			snippet += "burstBW: " + CircuitStatus.bandwidthToString(desc.burstBw) + "\n";
			snippet += "averageBW: " + CircuitStatus.bandwidthToString(desc.avgBw) + "\n";
			snippet += "observedBW: " + CircuitStatus.bandwidthToString(desc.observedBw) + "\n";
			
			GeoPoint gp = desc.getGeoPoint();
			if(gp != null) {
				OverlayItem item = new OverlayItem(desc.getGeoPoint(), title, snippet);
				rOverlay.addOverlay(item);
				lOverlay.addOverlay(item);
			}
		}
		
		
		mapOverlays.add(lOverlay);
		mapOverlays.add(rOverlay);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		Debug.stopMethodTracing();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	private class LinesItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	    public LinesItemizedOverlay(Drawable defaultMarker) {
	    	super(boundCenterBottom(defaultMarker));
	    }


	    public LinesItemizedOverlay(Drawable defaultMarker, Context context) {
	    	super(boundCenterBottom(defaultMarker));
	    	mContext = context;
	    }
	    
        public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
        }
        
	    @Override
		protected OverlayItem createItem(int i) {
			return mOverlays.get(i);
		}
	    
	    @Override
	    public void draw(	android.graphics.Canvas canvas,
                			MapView mapView,
                			boolean shadow) {
	    	Projection projection = mapView.getProjection();
	    	
    		float points[] = new float[(mOverlays.size() - 1) * 4];
    		for(int i = 0; i < mOverlays.size() - 1; i++) {
	    		Point startPoint = new Point();
	    		projection.toPixels(mOverlays.get(i).getPoint(), startPoint);
	    		
	    		points[i*4] = startPoint.x;
	    		points[i*4 + 1] = startPoint.y;
	    		
	    		Point endPoint = new Point();
	    		projection.toPixels(mOverlays.get(i + 1).getPoint(), endPoint);

	    		points[i*4 + 2] = endPoint.x;
	    		points[i*4 + 3] = endPoint.y;

	    	}
    		
    		Paint paint = new Paint();
    		paint.setColor(Color.BLACK);
    		
    		canvas.drawLines(points, paint);
	    }

		@Override
		public int size() {
			return mOverlays.size();
		}

		private Context mContext;		
		private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	}
	
	private class RouterItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	    public RouterItemizedOverlay(Drawable defaultMarker) {
	    	super(boundCenterBottom(defaultMarker));
	    }


	    public RouterItemizedOverlay(Drawable defaultMarker, Context context) {
	    	super(boundCenterBottom(defaultMarker));
	    	mContext = context;
	    }
	    
        public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
        }
        
	    @Override
		protected OverlayItem createItem(int i) {
			return mOverlays.get(i);
		}

		@Override
		public int size() {
			return mOverlays.size();
		}

		@Override
		public boolean onTap(int index) {
			
			  OverlayItem item = mOverlays.get(index);
			  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			  dialog.setIcon(R.drawable.tor25);
			  dialog.setTitle(item.getTitle());
			  dialog.setMessage(item.getSnippet());
			  dialog.show();
			  return true;
		}

		private Context mContext;		
		private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	}
	
	LinearLayout infoWindow;
	MapView mapView;
	List<Overlay> mapOverlays;
	Circuit circuit;
}
